//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   COUNTRY
// AUTHOR           :   Craig Chun
// DATE CREATE      :   2007/07/12
// DESCRIPTION      :   a MAINTENANCE ACTION file for the function, COUNTRY
//                  :
//                  :
//                  :
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2007/07/12   Craig Chun      1.0                 source is generated.
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.erp.app.setup.country.web;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.common.ProjectConstants;
import com.erp.app.setup.country.criteria.CountryHdrSrchCriteria;
import com.erp.app.setup.country.pk.CountryHdrPk;
import com.erp.app.setup.country.schema.CountryHdrSchema;
import com.erp.app.setup.country.vo.CountryHdrVo;
import com.erp.app.util.web.WebUtil;
import com.erp.common.web.SearchResultRow;
import com.erp.framework.util.BeanUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;
import com.erp.framework.util.user.UserDelegate;
import com.erp.framework.controller.action.SimpleMaintStyleActionTemplate;
import com.erp.framework.model.delegate.SimpleSearchMaintStyleDelegate;

/**
* A <strong>Maintenance Action</strong> is an adapter between the contents of an incoming
* HTTP request and the corresponding business logic that should be executed to
* process this request.  The controller (ActionServlet) will select an
* appropriate Action for each request.
* The main purpose for this action class is to handle the user document maintenance events
* from the web browsers.
*
* @author          Craig Chun
* @version 1.0     2007/07/12
* @since           FrameWork 1.0
*
*/
public class CountryMaintenanceAction extends SimpleMaintStyleActionTemplate {

	////////////////////////////////////////////////////////////////////////////////
	// CONSTANTS DEFINITION
	////////////////////////////////////////////////////////////////////////////////

	public static final int TAB_IDX_HDR = -1;
	public static final int TAB_IDX_PORT = 0;

	public static final String HDR = "Hdr";
	public static final String PORT = "Port";

	public static final String ACTION_CHANGE_FOLDER_HDR = ACTION_CHANGE_FOLDER + HDR;
	public static final String ACTION_CHANGE_FOLDER_PORT = ACTION_CHANGE_FOLDER + PORT;

	public static final String ACTION_ADD_PORT = ACTION_ADD_ROW + PORT;

	public static final String ACTION_DEL_PORT = ACTION_DELETE_ROW + PORT;


	////////////////////////////////////////////////////////////////////////////////
	// GLOBAL VARIABLES DEFINITION
	////////////////////////////////////////////////////////////////////////////////



	////////////////////////////////////////////////////////////////////////////////
	// LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////


	public ActionForward processActionAddPort(ActionMapping mapping, ActionForm formIn,
			HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
		ActionForward actionForward = new ActionForward(mapping.getInput());
		CountryMaintenanceForm form = (CountryMaintenanceForm) formIn;
		CountryPortRow newRow = (CountryPortRow) WebUtil.addRow(form, this.getChildRowClass(), this.getPk(form, request));
		newRow.setPortStatus(ProjectConstants.GENERIC_ACTIVE);
		// TODO Assign Pk fields from from into the new row
		return actionForward;
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// OVERRIDES PARENT'S OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////

	protected void beforeCreate(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		CountryMaintenanceForm form = (CountryMaintenanceForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			form.setCompanyCode(WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_COUNTRY));
			form.setTs(new CountryHdrSchema());
			form.setTsName("com.erp.app.setup.country.schema.CountryHdrSchema");
			form.setHdrStatus(ProjectConstants.GENERIC_ACTIVE);
		} catch (Exception e) {
			Logger.logExceptions(this, e);



		}
	}

	public String getSessionMaintStatus() {

		return getSessionSimpleMaintStatus();
	}

	public String getSessionMaintPk() {

		return getSessionSimpleMaintPk();
	}

	public String getStrutsId() {

		return getFunctionId();
	}

	public String getSimpleFormReadOnlySessionName(){

		//TODO: Add the following line to ProjectWebConstants.java
		//public static final String SESSION_COUNTRY_FORM_READ_ONLY_SESSION_NAME = "SESSION_COUNTRY_FORM_READ_ONLY_SESSION_NAME";
		return ProjectConstants.SESSION_COUNTRY_FORM_READ_ONLY_SESSION_NAME;
	}

	public String getSimpleFormReadOnlyCallerSessionName(){

		//TODO: Add the following line to ProjectWebConstants.java
		//public static final String SESSION_COUNTRY_FORM_READ_ONLY_CALLER_SESSION_NAME = "SESSION_COUNTRY_FORM_READ_ONLY_CALLER_SESSION_NAME";
		return ProjectConstants.SESSION_COUNTRY_FORM_READ_ONLY_CALLER_SESSION_NAME;
	}

	protected String getSimpleSrchMenuId() {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_COUNTRY = "MOD_COUNTRY";
		return ProjectConstants.MOD_COUNTRY;
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// IMPLEMENTS PARENT'S ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////

	protected String getModuleName(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request,HttpServletResponse response) {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_COUNTRY = "MOD_COUNTRY";
		return ProjectConstants.MOD_COUNTRY;
	}

	protected String getFunctionId() {

		//struts-config xxxxx.do
		return "countryMaintenance.do";
	}

	protected Class getMasterFormClass() {

		//the corresponding form class
		return CountryMaintenanceForm.class;
	}

	protected Class getMasterVOClass() {

		//corresponding master vo class
		return CountryHdrVo.class;
	}

	protected String getMasterFormSessionName() {

		return "countryMaintenanceForm";
	}

	protected Class[] getChildRowClass() {

		Class[] childRowClass = new Class[] {

			com.erp.app.setup.country.web.CountryPortRow.class
		};

		return childRowClass;
	}

	protected String[] getFolderChildIdxMapping() {

		String[] folderChildIdx = new String[2];

		folderChildIdx[0] = String.valueOf(TAB_IDX_PORT);
		folderChildIdx[1] = String.valueOf(TAB_IDX_PORT);

		return folderChildIdx;
	}

	protected SimpleBasePk getPk(ActionForm formIn, HttpServletRequest request) {

		CountryMaintenanceForm form = (CountryMaintenanceForm) formIn;
		CountryHdrPk pk = null;
		HttpSession session = request.getSession(false);

		//TODO: Add the following line to ProjectSecurityConstants.java
		//public static final String SP_SHARE_COUNTRY = "SP_SHARE_COUNTRY";
		String companyCode = WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_COUNTRY);

//		edit the following with appropriate form field names
		pk = new CountryHdrPk(companyCode, form.getCountryCode());
		return pk;
	}

	protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
		HttpServletResponse response, ActionErrors errors){

		CountryMaintenanceForm form = (CountryMaintenanceForm) formIn;
		form.setFormType(ProjectConstants.FORM_TYPE_MAINT);
	}

	protected void initAction(ActionMapping mapping, ActionForm formIn, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		super.initAction(mapping, formIn, request, response);
		HttpSession session = request.getSession(false);
        CountryMaintenanceForm form = (CountryMaintenanceForm) formIn;
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        try {
            HashMap errMsgMap = WebUtil.getModuleErrorMessages(editingUser.getCompanyCode(), DBMENU_GENERAL_MENU, form
                    .getModuleName(), form.getSysLangCode(), DBMENU_OBJECT_TYPE_ERR_MSG);
            form.setErrMsgMap(errMsgMap);

        } catch (Exception e) {
            Logger.logExceptions(this, e);
        }
	}



	////////////////////////////////////////////////////////////////////////////////
	// END OF ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////
}
